package com.swt.gap.common;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * @author sx 改编
 */
@Data
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> implements Serializable {

	private static final long serialVersionUID = 5408818226800883296L;

	@ApiModelProperty(value = "总记录数")
	private int totalRecord;

	@ApiModelProperty(value = "每页记录数")
	private int pageSize;

	@ApiModelProperty(value = "总页数")
	private int totalPage;

	@ApiModelProperty(value = "当前页数")
	private int pageNum;

    @ApiModelProperty(value = "列表数据")
	private List<T> list;
	
	/**
	 * 分页
	 * @param list        列表数据
	 * @param totalRecord  总记录数
	 * @param pageSize    每页记录数
	 * @param pageNum    当前页数
	 */
	public PageResult(List<T> list, int totalRecord, int pageSize, int pageNum) {
		this.list = list;
		this.totalRecord = totalRecord;
		this.pageSize = pageSize;
		this.pageNum = pageNum;
		this.totalPage = (int) Math.ceil((double)totalRecord/pageSize);
	}

	/**
	 * 分页
	 */
	public PageResult(Page<T> page) {
		this.list = page.getResult();
		this.totalRecord = (int)page.getTotal();
		this.pageSize = page.getPageSize();
		this.pageNum = page.getPageNum();
		this.totalPage = page.getPages();
	}

	/**
	 * 分页
	 */
	public PageResult(PageInfo<T> page) {
		this.list = page.getList();
		this.totalRecord = (int)page.getTotal();
		this.pageSize = page.getPageSize();
		this.pageNum = page.getPageNum();
		this.totalPage = page.getPages();
	}
}
